package com.example.server.controller;
import com.example.server.entity.Manager;
import com.example.server.service.ManagerService;
import com.example.server.util.Util;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Member;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("manager")
@CrossOrigin("http://localhost:5173")
public class ManagerController extends BaseController{
    private final ManagerService managerService;
    public ManagerController(ManagerService managerService) {
        this.managerService = managerService;
    }
    @GetMapping("queryAll")
    @ResponseBody
    private PageInfo<Manager> queryAll(){

        String token = request.getHeader("Authorization");
        System.out.println("token:" + token);
        if(Util.parse(token) == null){// token 认证失败 token 错误  token 为空  token 超时
            return null;
        }
        List<Manager> managers = managerService.queryAll();
        return new PageInfo<>(managers);
    }
    @PostMapping("create")
    @ResponseBody
    private Manager create(@RequestBody Manager manager){
       managerService.create(manager);
       Manager resultManager = new Manager();
       resultManager.setMid(manager.getMid());
       resultManager.setName(manager.getName());
       resultManager.setEmail(manager.getEmail());
       resultManager.setTelephone(manager.getTelephone());
       resultManager.setPassword(manager.getPassword());
        return resultManager;
    }
    @PostMapping("queryByAccountAndPassword")
    @ResponseBody
    private String queryByAccountAndPassword(@RequestBody Map<String,String> map){
         Manager manager = managerService.queryOne("queryByAccountAndPassword",map);
        if(manager != null){
            return Util.generate(manager.getName());
        }
        return null;
    }


}
